Public Class frmBaoCaoNhapXuatTheoDonVi
    Dim DonVi_ID As String = ""

    Private Sub frmBaoCaoNhapXuatTheoDonVi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        rbtKyQT.Checked = True
        KhoiTaocbbKyQuyetToan()
        cbbNhapXuat.SelectedIndex = 0
        cbbKYQT.SelectedIndex = 0
        rbtSLKH.Checked = True
        KhoiTaoDMDonVi()
    End Sub

    Public Sub KhoiTaoDMDonVi()
        Dim ary As ArrayList
        ary = CDoiTuongs.GetDoiTuongList(enumDOITUONG.DONVI)
        '
        For Each item As CDoiTuong In ary
            clbxDonVi.Items.Add(item)
        Next
    End Sub

    Public Sub KhoiTaocbbKyQuyetToan()
        Dim ds As DataSet = BaseDB.ExecSql_DataSet("select * from tblKyQT")
        cbbKYQT.DisplayMember = "tenkyqt"
        cbbKYQT.ValueMember = "id"
        Dim row As DataRow
        row = ds.Tables(0).NewRow
        row("id") = 0
        row("tenkyqt") = "Cả năm"
        ds.Tables(0).Rows.Add(row)
        cbbKyQT.DataSource = ds.Tables(0)
    End Sub

    Private Sub btnBaoCao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBaoCao.Click
        If (KiemTraNhap()) Then
            Dim loai_SLBaoCao, tieude, NX As String
            tieude = txtTieuDe.Text.ToUpper
            If (rbtSLKH.Checked) Then
                loai_SLBaoCao = "SLKH"
            Else
                loai_SLBaoCao = "SLTH"
            End If
            '
            Dim KyQT As String
            KyQT = cbbKYQT.SelectedValue
            Dim KyQT_BaoCao As String
            If (cbbKYQT.SelectedValue = 0) Then 'ca nam
                KyQT_BaoCao = "Năm " + CStr(Now.Year)
            Else
                KyQT_BaoCao = cbbKYQT.Text + " - Năm " + CStr(Now.Year)
            End If

            If (cbbNhapXuat.SelectedIndex = 0) Then
                NX = "N"
            Else
                NX = "X"
            End If

            Dim strsql As String
            strsql = "select MA, TEN, DVT, GLE, SUM(SLTH) as SLTH, SUM(SLKH) as SLKH,(GLE*SUM(SLTH)) AS THANHTIENTH,(GLE*SUM(SLKH)) AS THANHTIENKH " & _
                                  "from viewSLNX " & _
                                  "where NX = '" & NX & "'"
            If rbtKyQT.Checked Then
                If (cbbKYQT.SelectedValue <> 0) Then 'KyQT = Ca Nam
                    strsql &= " and KYQT = '" & cbbKYQT.SelectedValue & "'"
                End If
            Else
                strsql &= " AND NGAY >= '" & dtpTuNgay.Value.ToString("MM/dd/yyyy") & "' AND NGAY <= '" & dtpDenNgay.Value.ToString("MM/dd/yyyy") & "'"
            End If
            If (clbxDonVi.CheckedItems.Count <> 0) Then
                strsql &= " and( "
            End If

            Dim stt As Integer = 1
            For Each itemChecked As Object In Me.clbxDonVi.CheckedItems
                Dim objItem As CDoiTuong = itemChecked
                If (stt = 1) Then
                    strsql &= " DVGN = '" & objItem.KH & "'"
                Else
                    strsql &= " or DVGN = '" & objItem.KH & "'"
                End If
                stt += 1
            Next
            If (clbxDonVi.CheckedItems.Count <> 0) Then
                strsql &= " )"
            End If
            '
            strsql &= "group by MA,TEN,DVT,GLE order by TEN"
            Dim ds As DataSet
            ds = BaseDB.ExecSql_DataSet(strsql)
            Dim tongtien As Double
            Dim row As DataRow
            For Each row In ds.Tables(0).Rows
                If (rbtSLKH.Checked) Then
                    tongtien += row.Item("THANHTIENKH")
                Else
                    tongtien += row.Item("THANHTIENTH")
                End If
            Next
            Dim nnreport As New rptBaoCaoKetQuaNhapHang_XN150
            nnreport.SetDataSource(ds.Tables(0))
            nnreport.SetParameterValue("tien", DoiSoRaChu(tongtien))
            nnreport.SetParameterValue("SL_BaoCao", loai_SLBaoCao)
            nnreport.SetParameterValue("tieude", tieude)

            If rbtKyQT.Checked Then
                nnreport.SetParameterValue(1, KyQT_BaoCao)
            Else
                nnreport.SetParameterValue(1, "Từ ngày " + dtpTuNgay.Value.ToString("dd/MM/yyyy") + " đến ngày " + dtpDenNgay.Value.ToString("dd/MM/yyyy"))
            End If

            Dim frm As New frmReportViewer(nnreport)
            frm.ShowDialog()
        End If
    End Sub

    Function KiemTraNhap() As Boolean
        Dim kq As Boolean = True
        If (txtTieuDe.Text = "") Then
            ShowMessages("Bạn chưa nhập Tên tiêu đề cần báo cáo")
            kq = False
        Else
            If (clbxDonVi.CheckedItems.Count = 0) Then
                ShowMessages("Bạn chưa chọn đơn vị cần báo cáo")
                kq = False
            End If
        End If
        Return kq
    End Function

    Private Sub rbtKyQT_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtKyQT.CheckedChanged
        If rbtKyQT.Checked Then
            lblTuNgay.Visible = False
            lblDenNgay.Visible = False
            dtpTuNgay.Visible = False
            dtpDenNgay.Visible = False
            lblKyQT.Visible = True
            cbbKYQT.Visible = True
        Else
            lblTuNgay.Visible = True
            lblDenNgay.Visible = True
            dtpTuNgay.Visible = True
            dtpDenNgay.Visible = True
            lblKyQT.Visible = False
            cbbKYQT.Visible = False
        End If
    End Sub
End Class